package com.chaodev.base.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.chaodev.base.dao.UdDao;
import com.chaodev.base.model.FileInfo;
import com.chaodev.base.model.Student;
import com.chaodev.base.service.UploadDownloadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;

/**
 * @author lccccc
 */
@Service
public class UploadDownloadServiceImpl implements UploadDownloadService {
    @Autowired
    private UdDao udDao;

    @Value("${platform}")
    private String platform;

    @Override
    public boolean upload(List<Student> fileResultListList) {
        List<Student> list = new ArrayList<>();
        int num = 0;
        if ("mysql".equals(platform)){
            // mysql 10000条以上开始分批
            if (fileResultListList.size() > 10000) {
                for (Student student : fileResultListList) {
                    list.add(student);
                    if (list.size() == 10000) {
                        num += udDao.upload(list);
                        list.clear();
                    }
                }
                // 循环完后再提交一次
                num += udDao.upload(list);
                list.clear();
            } else {
                num = udDao.upload(fileResultListList);
            }
        } else {
            // oracle 2000条以上开始分批
            if (fileResultListList.size() > 2000) {
                for (Student student : fileResultListList) {
                    list.add(student);
                    if (list.size() == 2000) {
                        num += udDao.upload(list);
                        list.clear();
                    }
                }
                // 循环完后再提交一次
                num += udDao.upload(list);
                list.clear();
            } else {
                num = udDao.upload(fileResultListList);
            }
        }
        return num > 0;
    }

    @Async("importExecutor")
    @Override
    public CompletableFuture<Integer> uploadThread(List<Student> fileResultListList) {
        int num =  udDao.upload(fileResultListList);
        return CompletableFuture.completedFuture(num);
    }

    @Override
    public List<Student> findAll(String uuid) {
        return udDao.findAll(uuid);
    }

    @Override
    public List<Student> findByIdTemp(String uuid) {
        return udDao.findByIdTemp(uuid);
    }

    @Override
    public boolean insetInfo(FileInfo fileInfo) {
        int num = udDao.insetInfo(fileInfo);
        return num > 0;
    }

    @Override
    public boolean insertData(String id) {
        int num = udDao.insertData(id);
        return num > 0;
    }

    @Override
    public PageInfo<FileInfo> getAllFileInfo(Map<String, Object> map) {
        PageHelper.startPage(Integer.parseInt(map.get("P_PAGE").toString()), Integer.parseInt(map.get("P_PAGESIZE").toString()));
        List<FileInfo> list = udDao.getAllFileInfo(map);
        return new PageInfo<>(list, Integer.parseInt(map.get("P_PAGESIZE").toString()));
    }

    @Override
    public PageInfo<Student> findById(Map<String, Object> map) {
        PageHelper.startPage(Integer.parseInt(map.get("P_PAGE").toString()), Integer.parseInt(map.get("P_PAGESIZE").toString()));
        List<Student> list = udDao.findById(map);
        return new PageInfo<>(list, Integer.parseInt(map.get("P_PAGESIZE").toString()));
    }

    @Override
    public int getNnm(String uuid) {
        return  udDao.getNnm(uuid);
    }
}
